package org.softmed.rest.admin.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.softmed.filehandling.FileUtil;

public class FileManagerProxy implements FileManager {

	FileUtil util = new FileUtil();

	@Override
	public void createDirectory(String path) {
		File file = new File(path);// util.getFile(path);
		if (file.exists())
			throw new RuntimeException("Directory " + path
					+ " already exists !!!");

		boolean created = file.mkdir();
		if (!created)
			throw new RuntimeException("Couldn't create directory " + path);
	}

	@Override
	public void createFile(String path) throws Throwable {
		File file = new File(path);// util.getFile(path);
		if (file.exists())
			throw new RuntimeException("File " + path + " already exists !!!");

		boolean created = file.createNewFile();
		if (!created)
			throw new RuntimeException("Couldn't create file " + path);
	}

	@Override
	public DirectoryContents getDirectoryContent(String path) throws Throwable {
		DirectoryContents contents = new DirectoryContents();
		File file = new File(path);// util.getFile(path);
		File[] files = file.listFiles();
		ArrayList<String> directories = new ArrayList<String>();
		String filePath = null;
		int index = file.getCanonicalPath().length();//util.getRootPath().length
		// ();
		if (files != null)
			for (File file2 : files) {
				if (file2.isDirectory()) {
					filePath = file2.getPath();
					// filePath = util.fixPath(filePath.substring(index));
					contents.getDirectoryList().add(filePath);
				} else {
					filePath = file2.getPath();
//					 filePath = util.fixPath(filePath.substring(index));
					contents.getFileList().add(filePath);
				}

			}
		return contents;
	}

	@Override
	public String readFile(String path) throws Throwable {
		File file = new File(path);// util.getFile(path);
		String text = util.readFromFile(file);
		return text;
	}

	@Override
	public void remove(String path) throws Throwable {
		File file = new File(path);// util.getFile(path);
		boolean deleted = delete(file);
		if (!deleted)
			throw new RuntimeException("couldn't delete  " + path);
	}

	public static boolean delete(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = delete(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}

		// The directory is now empty so delete it
		return dir.delete();
	}

	@Override
	public void saveToFile(String path, String text) throws Throwable {
		File file = new File(path);// util.getFile(path);
		util.saveToFile(file, text);

	}

	@Override
	public void rename(String fullPath, String newName) throws Throwable {
		File file = new File(fullPath);// util.getFile(fullPath);
		if (!file.exists())
			throw new RuntimeException("File " + fullPath + " doesn't exist ! ");

		String dirPath = "/";

		int index = fullPath.lastIndexOf('/');
		if (index > 0) {
			String name = fullPath.substring(index + 1);
			dirPath = fullPath.substring(0, index + 1);
			System.out.println(dirPath);
			System.out.println(name);
		}

		String newFileNamePath = dirPath + newName;
		System.out.println(newFileNamePath);
		File newFile = new File(newFileNamePath);//util.getFile(newFileNamePath)
		// ;
		if (newFile.exists())
			throw new RuntimeException("File " + newFileNamePath
					+ " already exists !!!!");

		boolean renamed = file.renameTo(newFile);
		if (!renamed)
			throw new RuntimeException("File " + fullPath
					+ " couldn't be renamed to " + newName);

	}

	@Override
	public void move(String oldPath, String newPath) throws Throwable {
		File origin = new File(oldPath);// util.getFile(oldPath);

		if (!origin.exists() && !origin.equals("/"))
			throw new RuntimeException("Source " + newPath
					+ " doesn't exist!!!");

		File destination = new File(newPath);// util.getFile(newPath);

		boolean success = origin.renameTo(destination);

		if (!success)
			throw new RuntimeException("Couldn't move " + oldPath + " to "
					+ newPath);
	}

}
